package com.danan.realtime.app.dwd;

import com.danan.realtime.app.BaseSqlApp;
import com.danan.realtime.common.Constant;
import com.danan.realtime.util.SinkUtil;
import com.danan.realtime.util.SourceUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.hadoop.io.WritableComparator;
import scala.collection.immutable.Stream;

import java.time.Duration;

/**
 * @author NanHuang
 * @Date 2023/1/25
 */
public class DwdApp_4_trade_order_cancel_detail extends BaseSqlApp {
    public static void main(String[] args) {
        new DwdApp_4_trade_order_cancel_detail().init(4004,3,"DwdApp_4_trade_order_cancel_detail");
    }
    @Override
    public void invoke(StreamExecutionEnvironment env, StreamTableEnvironment tableEnv) {
        // 1 读取ods_db的数据
        readOdsDb(tableEnv,"DwdApp_4_trade_order_cancel_detail");
        // 2 过滤取消订单数据
        filterCanceledOrderInfo(tableEnv);
        // 3 读取下单详情数据
        readOrderDetail(tableEnv);
        // 4 join两表
        join(tableEnv);
        // 5 将数据写入Kafka
        writeToKafka(tableEnv);

    }

    private void writeToKafka(StreamTableEnvironment tableEnv) {
        tableEnv.executeSql("create table target (" +
                "id string," +
                "order_id string," +
                "user_id string," +
                "sku_id string," +
                "sku_name string," +
                "province_id string," +
                "activity_id string," +
                "activity_rule_id string," +
                "coupon_id string," +
                "date_id string," +
                "cancel_time string," +
                "source_id string," +
                "source_type_code string," +
                "source_type_name string," +
                "sku_num string," +
                "split_original_amount string," +
                "split_activity_amount string," +
                "split_coupon_amount string," +
                "split_total_amount string," +
                "ts bigint " +
                ")" +
                SinkUtil.getKafkaWith(Constant.TOPIC_DWD_TRADE_CANCEL_DETAIL));
        tableEnv.executeSql("insert into target select * from join_result");
    }

    private void join(StreamTableEnvironment tableEnv) {
        // 设置数据的TTL(30分钟业务时间 + 5秒网络传输延迟)
        tableEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(30 * 60 + 5));
        // join
        Table joinResult = tableEnv.sqlQuery("select " +
                "od.id," +
                "od.order_id," +
                "co.user_id," +
                "od.sku_id," +
                "od.sku_name," +
                "co.province_id," +
                "od.activity_id," +
                "od.activity_rule_id," +
                "od.coupon_id," +
                "date_format(co.operate_time, 'yyyy-MM-dd') operate_date_id," +
                "co.operate_time," +
                "od.source_id," +
                "od.source_type," +
                "od.source_type_name," +
                "od.sku_num," +
                "od.split_original_amount," +
                "od.split_activity_amount," +
                "od.split_coupon_amount," +
                "od.split_total_amount," +
                "co.ts " +
                "from cancel_order co " +
                "join order_detail od on co.id = od.order_id");
        tableEnv.createTemporaryView("join_result",joinResult);
    }

    private void readOrderDetail(StreamTableEnvironment tableEnv) {
        tableEnv.executeSql("create table order_detail (" +
                "id string," +
                "order_id string," +
                "user_id string," +
                "sku_id string," +
                "sku_name string," +
                "province_id string," +
                "activity_id string," +
                "activity_rule_id string," +
                "coupon_id string," +
                "date_id string," +
                "create_time string," +
                "source_id string," +
                "source_type string," +
                "source_type_name string," +
                "sku_num string," +
                "split_original_amount string," +
                "split_activity_amount string," +
                "split_coupon_amount string," +
                "split_total_amount string," +
                "ts bigint " +
                ")" +
                SourceUtil.getKafkaWith(Constant.TOPIC_DWD_TRADE_ORDER_DETAIL,"DwdApp_4_trade_order_cancel_detail"));
    }

    private void filterCanceledOrderInfo(StreamTableEnvironment tableEnv) {
        Table cancelOrderInfo = tableEnv.sqlQuery("select " +
                "data['id'] id," +
                "data['user_id'] user_id," +
                "data['province_id'] province_id," +
                "data['operate_time'] operate_time," +
                "ts " +
                "from source " +
                "where `database`='gmall2023' and `table`='order_info' " +
                "and `type`='update' " +
                "and `old`['order_status'] is not null " +
                "and `data`['order_status']='1003'");
        tableEnv.createTemporaryView("cancel_order",cancelOrderInfo);
    }
}
